﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace GameAutomationLibrary
{
    /// <summary>
    /// 
    /// </summary>
    public class HttpRequestHandler
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cookies"></param>
        /// <returns></returns>
        public static string HttpGet(string url,CookieContainer cookies)
        {

            //准备提交请求
            HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            webRequest.Method = "Get";
            //附加前面保持的cookie
            webRequest.CookieContainer = cookies;
            //提交请求获得响应
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            cookies.Add(webResponse.Cookies);
            //抽取响应内容
            StringBuilder strBuff = new StringBuilder();
            using (Stream respStream = webResponse.GetResponseStream())
            {
                using (StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8))
                {
                    strBuff.Append(respStreamReader.ReadToEnd());
                }
            }
            //todo:处理结果
            return strBuff.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <param name="cookies"></param>
        /// <returns></returns>
        public static string HttpPost(string url,string postData,CookieContainer cookies)
        {

            //准备提交请求
            HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            webRequest.Method = "Post";
            //附加前面保持的cookie
            webRequest.CookieContainer = cookies;
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] byte1 = encoding.GetBytes(postData);

            // Set the content type of the data being posted.
            webRequest.ContentType = "application/x-www-form-urlencoded";

            // Set the content length of the string being posted.
            webRequest.ContentLength = byte1.Length;

            using(Stream newStream = webRequest.GetRequestStream())
            {
                newStream.Write(byte1, 0, byte1.Length);
                // Close the Stream object.
                newStream.Close();
            }


            //提交请求获得响应
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            cookies.Add(webResponse.Cookies);
            //抽取响应内容
            StringBuilder strBuff = new StringBuilder();
            using (Stream respStream = webResponse.GetResponseStream())
            {
                using (StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8))
                {
                    strBuff.Append(respStreamReader.ReadToEnd());
                }
            }
            //todo:处理结果
            return strBuff.ToString();
        }
    }
}
